<?php

namespace app\weixin\controller;


use think\Db;
use think\Request;
use app\weixin\model\Shop as shopModel;
class Shop extends \think\Controller
{
    public function index()
    {

        //推荐产品
       
        $where['state'] = 3;
        $prolist = Db::table('product_1')->where($where)->order('rand()')->limit(10)->select();
        $this->assign('prolist', $prolist);


  



        //贡菜信息
        $cailist = Db::table('service_purchase')->join('unit', 'service_purchase.unit=unit.unid')->limit(5)->order('rand()')->select();
        $this->assign('cailist', $cailist);


        //推荐店铺
        $where['BusinessType']=1;
        $where['AttributeType']=3;

        $buylist = Db::table('product_business')->limit(3)->order('rand()')->select();
        $this->assign('buylist', $buylist);




        return view();


    }


    public function productlist()
    {
          


       $this->redirect('productlist1');

        $where['ShopType'] = 1;
        $where['State'] = 3;
        $list = Db::table('product')->where($where)->order('rand()')->limit(50)->select();
        $this->assign('list', $list);

        return view();


    }

    public function cate(){
     
        $list = Db::table('product_cate')->where($where)->order('sort desc')->limit(50)->select();
        $this->assign('list', $list);   
        return view();

    }


    public function productlist1()
    {
       
       $s=input('param.');


         if($s['cate']){

            $where['cate']=$s['cate'];

         }
         if($s['key']){

            $where['name']=['like',"%".$s['key']."%"];

         }

         $where['state'] = 3;
        $list = Db::table('product_1')->where($where)->order('rand()')->limit(50)->select();
        $this->assign('list', $list);

        return view();


    }

//产品详情页
    public function product()
    {

        $s = input('param.');
        $t = Db::table('product_1')->find($s['id']);
        if($t['url']){

         $this->redirect($t['url']);
        }

        if(!session('uid')){
          $this->redirect('weixin/index/login',['rurl'=>base64_encode(get_page_url())]);
       }

        $list=Db::table('product_price')->where(['pid'=>$s['id']])->order('price asc')->select();
        $this->assign('list',$list);
//        return json($list)
        $this->assign('t', $t);
        return view();
    }
//旅游首页
    public function tours()
    {
        $where['catename']=['in',"民俗风情,休闲娱乐"];
        $list1 = Db::table('tour_product')->where($where)->order('rand()')->limit(20)->select();
        $this->assign('list1', $list1);
        $where['catename']=['not in',"民俗风情,休闲娱乐"];
        $list2 = Db::table('tour_product')->where($where)->order('rand()')->limit(20)->select();
        $this->assign('list2', $list2);

        return view();


    }


     public function tourlist()
    {

        $where['catename']=input('catename');

        $list1 = Db::table('tour_product')->where($where)->limit(20)->select();
        $this->assign('list', $list1);

       

        return view();


    }


//旅游
    public function tour()
    {
        $s = input('param.');
        $t = Db::table('product')->find($s['id']);
        //$pic = Db::table('product_picurl')->where('FID', $s['id'])->select();
        $this->assign('pic', $pic);
        $this->assign('t', $t);
        return view();
    }


//社区
    public function community()
    {

        $where['ShopType'] = 2;
        $where['state'] = 3;
        $prolist = Db::table('product')->where($where)->order('rand()')->limit(8)->select();

        $buslist=Db::table('product_business')->where($where)->order('rand()')->limit(6)->select();

        $this->assign('buslist',$buslist);

        $this->assign('prolist', $prolist);
        return view();
    }


    //特产
    public function localproducts()
    {
        $where['ShopType'] = 3;
        $where['State'] = 3;
        $list = Db::table('product')->where($where)->order('rand()')->limit(20)->select();
        $this->assign('list', $list);

        return view();
    }


//搜9福
    public function so9(){

        $type=input('type','jd');
        $this->assign('type',$type);
        $where['state']=3;
        switch($type){

            case "jd":
                $list=Db::table('so9_gather')->where($where)->order('sort desc')->select();
                break;


            case "ms":
                $list=Db::table('so9_seckill')->where($where)->order('sort desc')->select();
                break;


            case "jsg":
                $list=Db::table('so9_hour')->where($where)->order('sort desc')->select();
                break;

            case "pt":
                 $list=Db::table('so9_together')->where($where)->order('sort desc')->select();
                break;



        }



        $script = &  load_wechat('Script');
        $qm = $script->getJsSign('http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
        $this->assign('qm',$qm);





        $this->assign('list',$list);

         $ad=Db::table('ad')->where("type","so9")->order('sort desc')->select();
         $this->assign('ad',$ad);


        return view();
    }



    //商家
    public function business(){

        $bid=input('bid');
        $t=Db::table('user_shop')->find($bid);

        $where['state']=3;
        $where['bid']=$bid;
        $list=Db::table('product_1')->where($where)->order('id desc')->select();

        $this->assign('t',$t);
        $this->assign('list',$list);
        return view();
    }





    //seckill

    public function seckill(){
         
         if(!session('wxid')){
          
          getwxinfo();

        }
        $s=input('param.');

        $t=Db::table('so9_seckill')->find($s['id']);
        Db::table('so9_seckill')->where('id',$s['id'])->setInc('click');

        $this->assign('t',$t);

        $xz=date('H-i-s');

        $xzzd=date('H')."-00-00";
        $xzjs=date('H')."-10-59";


     if($xz>$xzzd && $xz<$xzjs && date('H')>9 && date('H')<23){

         $msg="抢购中";
         $kqtime=date('Y/m/d H:i:s');  

      }else{
        $msg="未开始";
        $kqtime=date('Y/m/d H:00:00',strtotime("+1 hour"));

      }

     



         
    


         $this->assign('kqtime',$kqtime);
        $this->assign('msg',$msg);

    
        $script = &  load_wechat('Script');
        $qm = $script->getJsSign('http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
        $this->assign('qm',$qm);



        return view();




    }


    public function gather(){

        if(!session('wxid')){
          
          getwxinfo();

        }


        $s=input('param.');

        $t=Db::table('so9_gather')->find($s['id']);
        Db::table('so9_gather')->where('id',$s['id'])->setInc('click');

        $this->assign('t',$t);
     

     $script = &  load_wechat('Script');
        $qm = $script->getJsSign('http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
        $this->assign('qm',$qm);

        return view();
    }




    public function together(){

        if(!session('wxid')){
          
          getwxinfo();

        }


        $s=input('param.');

        $t=Db::table('so9_together')->find($s['id']);
        Db::table('so9_together')->where('id',$s['id'])->setInc('click');

        $this->assign('t',$t);
     

     $script = &  load_wechat('Script');
        $qm = $script->getJsSign('http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
        $this->assign('qm',$qm);

        return view();
    }




    public function hour(){

        if(!session('wxid')){
          
          getwxinfo();

        }


        $s=input('param.');

        $t=Db::table('so9_hour')->find($s['id']);
        Db::table('so9_hour')->where('id',$s['id'])->setInc('click');

        $this->assign('t',$t);
     

        $script = &  load_wechat('Script');
        $qm = $script->getJsSign('http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
        $this->assign('qm',$qm);

        
        $xz=date('H:i');
        $fz=date('H'); 
        if($xz>=$fz.":00"){

          $jg='s1';


        }

        if($xz>$fz.":14"){

            $jg='s2';



        }

        if($xz>$fz.":29"){


           $jg='s3';


        }

         if($xz>$fz.":44"){

            $jg='s4';



         }
     
      $this->assign('jg',$jg);


        return view();
    }
    public function hh()
    {
    	return $this->fetch();
    }
    //秒杀
    public function agsecondill()
    {
        $id = input('id');
        $res = Db::table('so9_seckill')->find($id);
        $this->assign('res',$res);
        return $this->fetch();
    }
    //处理秒杀
      //处理秒杀
    public function agsecondillHandel()
    {
         $row = Db::table('so9_seckill')->find($_POST['id']);
         $res = array('code'=>0,'msg'=>'error');
         if($_POST['pwd']!='阿盖花生' &&$row['flag'] =1 ) return json_encode($res);
         if($row['num'] ==0) return json_encode($res);
         if($row){
            Db::table('so9_seckill')->where('id',$_POST['id'])->setDec('num');
            $res = array('code'=>1,'msg'=>'success');
         }
        return json_encode($res);
    }
   //梨的秒杀
     public function li()
    {
        $id = input('id');

        $res = Db::table('so9_seckill')->find($id);
        $this->assign('res',$res);
        return $this->fetch();
    }
    //处理秒杀
      //处理秒杀
    public function limiaosha()
    {
         $row = Db::table('so9_seckill')->find($_POST['id']);
         $res = array('code'=>0,'msg'=>'error');
         if($row['num'] ==0) return json_encode($res);
         if($row){
            Db::table('so9_seckill')->where('id',$_POST['id'])->setDec('num');
            $res = array('code'=>1,'msg'=>'success');
         }
        return json_encode($res);
    }
    public function jjj()
    {
    	return $this->fetch();
    }
    public function dd()
    {
    	return $this->fetch();
    }
    public function goodsinfo()
    {
        
    }



}
